package module::modewalk_us::default;
use Encode;
use vars qw($VERSION @ISA @EXPORT);
$VERSION = 1.00;
@ISA     = qw(Module::Module);
@EXPORT  = ();
#use strict;

# ----------------------------------------------------
# constructor
# ----------------------------------------------------
sub new {
	my $class = shift;
	my %parm  = @_;
	my $this  = {};
	bless $this, $class;
	return $this;
}

# ----------------------------------------------------
# get all product's url and categories then write them
# into temp xml
# ----------------------------------------------------

sub filter {

	#A200000 Filter retailer website starts
	Log::Write::setLog('A200000');
	my $this     = shift;
	my $writexml = new Feed::XML();
	my $data     = $main::filedata;
	my $hostURL  = $this->{'config'}->{'host_url'};
	my $catalog  = { 'Product' => [] };
	my $charset  = 'UTF-8';
	# get categoryUrls
	my @categoryUrls;
 	@categoryUrls = &getCategoryUrls( $hostURL, $charset ,$data);
	if ( !@categoryUrls ) {
		# E200000: filter failed
		Log::Write::setLog('E200000');
	}
	else {
		# A200003: The total count of the entryUrl obtained
		Log::Write::setLog( 'A200003', { 'COUNT' => scalar(@categoryUrls) } );
	}
	
	# get all page urls
	$catalog->{'Product'} = &getEntryUrls( $charset, \@categoryUrls ,$data);

	# write tree date into XML file
	if ($catalog->{'Product'}) {
		$writexml->addXML( $catalog, $this->{'config'}->{'data_filter_file'} );
		# A200002the total count of detail page urls
		Log::Write::setLog('A200002', {'COUNT' => scalar(@{$catalog->{'Product'}})});
	}
	else {

		# E200000filter failed
		Log::Write::setLog('E200000');
	}

	# A299999Filter retailer website end.
	Log::Write::setLog('A299999');
}

# ----------------------------------------------------
# parse all products detail page and write the infos
# into final XML feed
# ----------------------------------------------------
sub parser {

	# A300000Parser detail page starts.
	Log::Write::setLog('A300000');
	my $this = shift;

	# new parser object
	my $parserObj = module::modewalk_us::parser->new;

	# new $writexml object, used for read and write XML info
	my $writeXml = new Feed::XML();

	#read xml create by filter to get all products infos(url and category)
	my @xmlInfo = $writeXml->readXML( $this->{'config'}->{'data_filter_file'} );

	my $count = 0;
	my $total = 0;

	# $catalog is used for recording a set of products
	my $catalog = { 'Product' => [] };

	my $hostUrl = $this->{'config'}->{'host_url'};
#	my @category;
	my $brandName = 'Modewalk US';
	for my $oneTemp (@xmlInfo) {

		# %ProductInfo : one product info from *_filter.xml
		my %oneTempInfo = %{$oneTemp};
		my $url         = ( @{ $oneTempInfo{"ProductURL"} } )[0];
		my %product;
		my %productInfo;
		my $category = $oneTempInfo{"Category"};
		
		#print @{ $oneTempInfo{'Category'} }[0];
		eval {

			# get infos from product detail page and save it in hash variable
			%productInfo = $parserObj->parseAndgetInfo( $url, $hostUrl );
		};
		
		if ($@) {

			# E300003 : Failed to extract information from XXX page.
			Log::Write::setLog( "E300003", { 'URL' => $url } );
			next;
		}
		if ( $productInfo{'ProductId'} ) {
			my $product = {
				'Retailer'    => $this->{'config'}->{'retailer_name'},
				'ProductId'   => $productInfo{'ProductId'},	
				'Name'        => $oneTempInfo{'Name'},		
				'Brand'       => $oneTempInfo{'Brand'},
				'Price'       => $productInfo{'Price'},
				'SalePrice'   => $productInfo{'SalePrice'},
				'Description' => $productInfo{'Description'},
				'ProductURL'  => $oneTempInfo{'ProductURL'},	
				'ImageURL'    => $productInfo{'ImageURL'},			
				'Category'    => $oneTempInfo{'Category'},			
				'Size'        => $productInfo{'Size'},
				'Time'        => $productInfo{'Time'}
			};

			$catalog->{'Product'} = [ @{ $catalog->{'Product'} }, $product ];

			$count++;
			$total++;

			# write data into XML per 100(configurable) records
			if ( $count == $this->{'config'}->{'write_frequency'} ) {
				$writeXml->addXML( $catalog,
					$this->{'config'}->{'data_feed_file'} );
				$catalog = { 'Product' => [] };

				# A300001 : Record XXX prodcuts have been appended to feed file.
				Log::Write::setLog( "A300001", { 'COUNT' => $count } );
				$count = 0;
			}
		}
	}
	$writeXml->addXML( $catalog, $this->{'config'}->{'data_feed_file'} );

	# A300001 : Record XXX prodcuts have been appended to feed file.
	Log::Write::setLog( "A300001", { 'COUNT' => $count } );

	if ($total) {

	   # A300003 : Record XXX prodcuts have been written into feed file totally.
		Log::Write::setLog( 'A300003', { 'COUNT' => $total } );

		# A399999 : Parser detail page end.
		Log::Write::setLog('A399999');
	}
	else {

		# E300000 : Parser failed.
		Log::Write::setLog('E300000');
	}
}

# ----------------------------------------------------
# get all category urls
# Modify Log
# Modify Louise 1/9/2013 #8857 ####
# Modify Louise 1/10/2013 #8857 ####
# ----------------------------------------------------

sub getCategoryUrls {
	my $url     = shift;
	my $charset = shift;
	my $data    = shift;
	my $content = $data->getPage( $url, $charset );
	my @categoryUrls;
	# get page failed
	if ( !$content ) {
		# E200001 Failed to get XX entrance information.
		Log::Write::setLog( 'E200001', { 'URL' => $url } );
		return;
	}

	if ( $content =~ /<div class="menu_wrapper">(.*?)<\/div>/is ){
		my $cat_content = $1;
		$cat_content =~ s/&#39;/'/isg;
		while ( $cat_content =~ /<li class="tab[^>]*?>\s*?<a href="([^"]*?)">\s*?<h3[^>]*?>(.*?)<\/h3>\s*?<\/a>(.*?)<\/ul>/isg ){
		    my $firstcat_url      = 'http://www.modewalk.com'.$1;
			my $firstcat_name     = $2;
			$firstcat_name =~ s/ <span>.*?<\/span>//isg;
			my $secondcat_content = $3;
			if ( $firstcat_name =~ /designers/is ){	
			    next;
			}
	        push(@categoryUrls, $firstcat_url."<split>".$firstcat_name);
	        Log::Write::setLog( 'A200001', { 'URL' => $firstcat_url."<split>".$firstcat_name} );		

	        if ( $secondcat_content =~ /style<\/h3>/is ){	
				next;
		    }
	        while ( $secondcat_content =~ /<li[^>]*?>\s*<a href="(.*?)">\s*<h3>(.*?)<\/h3>\s*<\/a>\s*<\/li>/isg ){
	            my $secondcat_url   = 'http://www.modewalk.com'.$1;
	            my $secondcat_name  = $2;            		
	            push(@categoryUrls, $secondcat_url."<split>".$firstcat_name."<subsplit>".$secondcat_name);
	            Log::Write::setLog( 'A200001', { 'URL' => $secondcat_url."<split>".$firstcat_name."<subsplit>".$secondcat_name} );	       	            			
	        }
	    }  		
	}
	return @categoryUrls;
}

# ----------------------------------------------------
# get all entry urls
# Modify Log
# ----------------------------------------------------
sub getEntryUrls {

	my $charset      = shift;
	my $entryUrls    = shift;
	my $data         = shift;
	my @entryUrlList = @{$entryUrls};
	my $temp;
	my $allProducts;

	####Modify BEGIN Louise 1/10/2013 #8857 ####	
	for my $entryUrlRef (@entryUrlList) {
		my ($url,$category) = split('<split>', $entryUrlRef);
		my @categorys = split("<subsplit>",$category);
		$url =~ s/&amp;/&/isg;
#		#### BEGIN Alex 10/30/2012 ####
#		if ( $url =~ /catalog\/$/is ){			
#			$url = $url.'?order=date_added&page=-1&sort=1';
#		}elsif( $url =~ /catalog/is ){
#			$url = $url.'&order=date_added&page=-1&sort=1';
#		}
		#### END Alex 10/30/2012 ####
		my $content = $data->getPage( $url, $charset );
		#get product list
		my $productCount = 0;
		if($content =~ /<div class="catalog[^"]*"[^>]*>(.*?)<div class="footer_global[^"]*"[^>]*>/is){
			$content = $1;
			while($content =~ /<div class=["']productcatalog["']>(.*?)<\/a>/isg) {
				my $temp = $1;
				#### the product sold out
				if($temp =~ /<div class="sold_out/is){
					next;
				}
				####Modify END Louise 1/9/2013 #8857 ####
				
				my $productUrl;
				if($temp =~ /<a href="(.*?)"\s*>/is){
					$productUrl = $1;
				}
				$productUrl =$data->trimURL($productUrl,$url);
				my $brand ;
				if($temp =~ /<div class='productcatalog_brand'>(.*?)<\/div>/is){
					$brand = $1;
				}
				my $name ;
				if($temp =~ /<div class='name'>(.*?)<\/div>/is){
					$name = $1;
				}
			    my $product = {
							'ProductURL'=> $productUrl,
							'Name'=> $brand?$brand.' '.$name:$name,
							'Brand' => $brand,
							'Category'=>[{'Part' => \@categorys}]};
				$allProducts = [ @{$allProducts },$product ];
				$productCount++;				
			}	
		}
		#### BEGIN Alex 10/30/2012 ####
		elsif( $content =~ /<div class="right-side">\s*(<div class="brand-page-product">.*?<\/div>\s*<\/div>)\s*<\/div>\s*<\/div>\s*<\/div>/is ){
			$content = $1;
			while( $content =~ /<div class="brand-page-product">(.*?)<\/div>\s*<\/div>/isg ) {
				my $temp = $1;
				#### the product sold out ####Modify BEGIN Louise 1/10/2013 #8857 ####
				if($temp =~ /<div class="sold-out/is){
					next;
				}
				####Modify END Louise 1/10/2013 #8857 ####
				
				my $productUrl;
				if($temp =~ /<a href="(.*?)"\s*>/is){
					$productUrl = $1;
				}
				$productUrl =$data->trimURL($productUrl,$url);
				my $brand ;
				if($temp =~ /<div class="catalog-brand-name">(.*?)<\/div>/is){
					$brand = $1;
				}
				my $name ;
				if($temp =~ /<div class="catalog-product-name">(.*?)<\/div>/is){
					$name = $1;
				}
			    my $product = {
							'ProductURL'=> $productUrl,
							'Name'=> $brand?$brand.' '.$name:$name,
							'Brand' => $brand,
							'Category'=>[{'Part' => \@categorys}]};
				$allProducts = [ @{$allProducts },$product ];
				$productCount++;				
			}				
		}
		#### END Alex 10/30/2012 ####
		Log::Write::setLog( 'A200001', { 'URL' => $url,'Count' =>$productCount });
	}
	return $allProducts;
}

1;
